package org.jeecg.modules.sxaq.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 定期任务
 * @Author: DHC
 * @Date:  2020-12-01
 */
@Slf4j
@Component
public class Scheduler {

    @Value(value="${sxaq.guest.hold-time}")
    private Integer holdTime;
    @Autowired
    private ISysUserService sysUserService;

    //@Scheduled(fixedRate = 2000) // 每2秒执行一次
    //@Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "0 0 1 * * ?")
    public void testTasks() {
        log.info("------------" + holdTime.intValue());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, holdTime.intValue()*-1);
        Date expireDate = cal.getTime();
        List<SysUser> expireUsers = sysUserService.queryExpireGuest(expireDate);
        sysUserService.removeGuests(expireUsers);
    }


    //@Scheduled(fixedRate = 2000) // 每2秒执行一次
    //@Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "0 0 1 * * ?")
    public void recordTasks() {
        log.info("------------" + holdTime.intValue());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, holdTime.intValue()*-1);
        Date expireDate = cal.getTime();
        List<SysUser> expireUsers = sysUserService.queryExpireGuest(expireDate);
        sysUserService.removeGuests(expireUsers);
    }
}